package com.example.demo.qrcode;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Author: zjq
 * @Date: create in 2022/10/13 10:58
 */
@Service
public class QrServiceImpl implements QrService {

    @Value("${tem-path}")
    private String path;
    @Value("${img-path}")
    private String imgpath;

    @Override
    public void createQrCode(int a) {
        for (int i = 0; i < a; i++) {
            try {
                //创建一个不会重复的UUID
                String uuid = UUID.randomUUID().toString();
                //去掉 - 并截取前8位
                String name = uuid.replace("-", "").substring(0, 8);
                String content = uuid;
                BufferedImage image = QRCodeUtil.createImage("utf-8", content, 500, 500,0xFFCC7D50);
                //6CB37F  6696C2 CC7D50 CD5154

                String c = imgpath + "2.png";
                //String c = imgpath + "favicon.png";
                System.out.println("===========" + c);
                Image img = ImageIO.read(new File(c));
                QRCodeUtil.insertLogoImage(image, img, 130, 130);
                //QRCodeUtil.textToImage("扫码获取答案", 50, 50, 30);
                QRCodeUtil.addUpFont(image, "博学");
                String formatName = "png";
                String imagePath = path + name + ".png";
                ImageIO.write(image, formatName, new File(imagePath));
                //ImageIO.write((RenderedImage) img, formatName, new File(imagePath));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("执行完成");
    }
}
